package com.alibaba.citrus.ots.forecast.common;

import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @description: 时间处理工具类
 * @author: qianlei
 * @create: 2021/11/14 18:33
 */
public class DateUtils {

    //毫秒
    public static final int MSEC = 1;
    //秒
    public static final int SEC = 1000;
    //分
    public static final int MIN = 60000;
    //时
    public static final int HOUR = 3600000;
    //天
    public static final int DAY = 86400000;
    /**
     * 默认格式
     */
    private static final DateFormat DEFAULT_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final String TIME_FORMAT = "HH:mm:ss";
    public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 日期时间 格式yyyy-MM-dd HH:mm:ss
     */
    public static String PATTEN_YMDSFM = "yyyy-MM-dd HH:mm:ss";

    public static String PATTEN_YMDSF = "yyyy-MM-dd HH:mm";
    public static String PATTEN_YMDS = "yyyy-MM-dd HH";
    /**
     * 日期格式yyyy-MM-dd
     */
    public static String PATTEN_YMD = "yyyy-MM-dd";
    /**
     * 日期格式yyyy年MM月dd日
     */
    public static String PATTEN_YMD_CN = "yyyy年MM月dd日";

    public static String PATTEN_SLASH_CN = "yyyy/MM/dd";

    /**
     * 日期格式yyyyMMdd
     */
    public static String PATTEN_YMD_PLAIN = "yyyyMMdd";

    /**
     * 日期格式 时分秒
     */
    public static final String PATTEN_HFM = "HH:mm:ss";
    /**
     * 日期格式 时
     */
    public static final String PATTEN_H = "HH";
    /**
     * 日期格式 分
     */
    public static final String PATTEN_M = "mm";

    /**
     * 默认的日期间隔符
     */
    public static String DEFAULT_SEPERATOR = "-";

    /**
     *  取00:00:00
     *
     * @param source
     * @param
     * @return
     */
    public static Date getFirstTime(String source) {
        if (StringUtils.isBlank(source)){
            return null;
        }
        Date date = string2Date(source);
        return getFirstTime(date);

    }

    /**
     * 取23:59:59
     * @param source
     * @return
     */
    public static Date getLastTime(String source) {
        if (StringUtils.isBlank(source)){
            return null;
        }
        Date date = string2Date(source);

        return getLatestTime(date);
    }


    public static Date getFirstTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 00);
        calendar.set(Calendar.MINUTE, 00);
        calendar.set(Calendar.SECOND, 00);
        calendar.set(Calendar.MILLISECOND,000);
        return calendar.getTime();
    }

    public static Date getLatestTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 获取SimpleDateFormat
     *
     * @param pattern
     * @return
     */
    private static SimpleDateFormat getDateParser(String pattern) {
        return new SimpleDateFormat(pattern);
    }

    /**
     * 字符串转换时间
     *
     * @param time
     * @return
     */
    public static Date string2Date(String time) {
        if (StringUtils.isBlank(time)){
            return null;
        }
        switch (time.length()){
            case 8:
                if (time.contains(":")){
                    return string2Date(time, PATTEN_YMD_PLAIN);
                }
                return string2Date(time, TIME_FORMAT);
            case 10:
                if (time.contains("年")){
                    return string2Date(time, PATTEN_YMD_CN);
                }
                return string2Date(time, PATTEN_YMD);
            case 13:
                return string2Date(time, PATTEN_YMDS);
            case 16:
                return string2Date(time, PATTEN_YMDSF);
            case 19:
                return string2Date(time, DATETIME_FORMAT);
            default:
                return null;
        }

    }

    public static Date string2Date(String time, String pattern) {
        try {
            return getDateParser(pattern).parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date string2Date(String time, DateFormat format) {
        if (StringUtils.isBlank(time)){
            return null;
        }
        try {
            return format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(firstDayOfMonth());
        System.out.println(endDayOfMonth());
//        System.out.println(string2Date("2022-02-02 14:09:33"));
        Date date = new Date();
        Date ss = string2Date("9999-01-01 00:00:00");
        System.out.println(date.equals(ss));
        System.out.println(date.equals(ss));
    }
    public static Date firstDayOfMonth(){
        Calendar  cal=Calendar.getInstance();//获取当前日期
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        return cal.getTime();

    }

    public static Date endDayOfMonth(){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY,23);
        cal.set(Calendar.MINUTE,59);
        cal.set(Calendar.SECOND,59);
        cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }

}
